package com.ftm.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ftm.tools.PageData;
import com.ftm.entity.Shopc;
import com.ftm.service.ShopCarService;
import com.ftm.service.impl.ShopCarServiceImpl;

/**
 * Servlet implementation class ShopCarServlet
 */
@WebServlet("/ShopCarServlet")
public class ShopCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShopCarServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at: ").append(request.getContextPath());
		// 1设置请求编码
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		//
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("showMyCar".equals(op)) {
			showMyCar(request, response);
		}
		System.out.println(op);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	private void showMyCar(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ShopCarService scs = new ShopCarServiceImpl();
		// 获取到用户id
		String userid = request.getSession().getAttribute("user") + "";

		int user = Integer.parseInt(userid);
		// 查询全部
		int page = 1;
		int pageSize = 10;
		String keywords = "";
		// 页码和关键字
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		PageData<Shopc> pd = scs.showShopCar(user, page, pageSize, keywords);
		List<Shopc> list = pd.getData();
		List<Shopc> shopname = new ArrayList<Shopc>();
		for (Shopc shopc : list) {
			shopname.add(scs.showShopName(shopc));
		}

		for (int i = 0; i < shopname.size(); i++) {
			Shopc s = shopname.get(i);
			System.out.println(s.getShopname());
		}
		if (shopname.size() == 0) {
			request.setAttribute("pd", "测试？");
			request.getRequestDispatcher("Cart.jsp").forward(request, response);
		} else {
			request.setAttribute("pd", pd);
			request.setAttribute("shopname", shopname);
			request.setAttribute("keyword", keywords);
			request.getRequestDispatcher("Cart.jsp").forward(request, response);
		}
		// <!-- <div class="cart_item_price">${goods.price}</div> -->

	}
}
